package mx.com.bancogato.fingersign;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import mx.com.mindbits.bandpay.R;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class DrawView extends View implements OnTouchListener {
		
	private List<Point> points = new ArrayList<Point>();
	private Paint paint = new Paint();
	boolean flag = false;
	private Context context;
	
	private Random gen;
	private int col_mode;
	private int wid_mode;
	private String[] text;
	
	public int getCol_mode() {
		return col_mode;
	}

	public void setCol_mode(int col_mode) {
		this.col_mode = col_mode;
	}

	public int getWid_mode() {
		return wid_mode;
	}

	public void setWid_mode(int wid_mode) {
		this.wid_mode = wid_mode;
	}

	public String[] getText() {
		return text;
	}

	public void setText(String[] text) {
		this.text = text;
	}

	public DrawView(Context context, String[] texto) {
		super(context);
		this.context = context;
		// set default color to white
		setCol_mode(ConstantsValues.COL_MODE);		
		// set default width to 7px
		setWid_mode(ConstantsValues.WID_MODE);
		setText( texto );
		setFocusable(true);
		setFocusableInTouchMode(true);
		this.setOnTouchListener(this);
		
		paint.setAntiAlias(true);
	}
	
	// Method used to clear the screen
	public void clearPoints () {
		points.clear();
		forceRedraw();
	}
	//Method add to detect if we have a draw or not
	public int showPoints(){
		return points.size();
	}
	
	/**
	 * Force view to redraw. Without this points aren't cleared until next action
	 */
	public void forceRedraw() {
		invalidate();
	}
	
	// used to set drawing colour
	public void changeColour (int col_in) {
		col_mode = col_in;
	}

	// used to set drawing width
	public void changeWidth (int wid_in) {
		wid_mode = wid_in;
	}
	
	@Override
	public void onDraw(Canvas canvas) {
		// for each point, draw on canvas
		for (Point point : points) {
			point.draw(canvas, paint);
			Log.d(ConstantsValues.TAG_DRAWVIEW, "pointcount: " + points.size());
		}
		textForSigned(canvas, text, paint);
		helpTextForSigned(canvas,paint);
	}
	
	public void textForSigned(Canvas canvas, String compra[], Paint p){
		p.setTextSize((float) (canvas.getHeight()*0.05));
		p.setColor(Color.argb(220,190, 190, 190));
		
		for(int i =0; i < compra.length; i++){
			
			canvas.drawText(compra[i], 10, 30*(i+1), p);
		}
		
	}
	public void helpTextForSigned(Canvas canvas, Paint pincel){
		pincel.setTextSize(60);
		pincel.setColor(Color.argb(120,190, 190, 190));
		pincel.setStrokeWidth(25);	
		
		canvas.drawText(context.getString(R.string.label_sign) ,
				(canvas.getWidth()/3 * 2) , 
				(canvas.getHeight()/3) * 2 , pincel);
	}
	
	public boolean getFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public boolean onTouch(View view, MotionEvent event) {
		int new_col = 0;
		if (getCol_mode() < 0) {
			gen = new Random();
			setCol_mode(gen.nextInt( 8 ));
		}
		// This if statement may be redundant now
		if (getCol_mode() >= 0) {
			switch (getCol_mode()) {
				case 0 : {
					new_col = Color.WHITE;
					break;
				}
				case 1 : {
					new_col = Color.BLUE;
					break;
				}
				case 2 : {
					new_col = Color.CYAN;
					break;
				}
				case 3 : {
					new_col = Color.GREEN;
					break;
				}
				case 4 : {
					new_col = Color.MAGENTA;
					break;
				}
				case 5 : {
					new_col = Color.RED;
					break;
				}
				case 6 : {
					new_col = Color.YELLOW;
					break;
				}
				case 7 : {
					new_col = Color.BLACK;
					break;
				}
			}
		}
		/* else {
			gen = new Random();
			new_col = gen.nextInt( 8 );
		} */
		Point point;
		if(event.getAction() == MotionEvent.ACTION_MOVE) {
			point = new FriendlyPoint(event.getX(), event.getY(), 
					new_col, points.get(points.size() - 1), wid_mode);	
			setFlag(true);
		} else if (event.getAction() == MotionEvent.ACTION_DOWN) {	
			point = new Point(event.getX(), event.getY(), new_col, wid_mode);
			setFlag(true);
		} else {
			return false;
		}
		points.add(point);
		forceRedraw();
		
		Intent intent = new Intent("UPDATE");
		intent.putExtra("numPoints", showPoints());
		context.sendBroadcast(intent);
		
		Log.d(ConstantsValues.TAG_DRAWVIEW, "point: " + point);
		return true;
	}
}
